<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车架号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车的名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入车的名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型" prop="vehicleTypeName">
        <el-input
          v-model="queryParams.vehicleTypeName"
          placeholder="请输入车型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆尺寸" prop="vehicleSize">
        <el-input
          v-model="queryParams.vehicleSize"
          placeholder="请输入车辆尺寸"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="vehicleStatus">
        <el-select v-model="queryParams.vehicleStatus" placeholder="请选择车辆状态" clearable>
          <el-option
            v-for="dict in dict.type.whole_dealer_vehicle_info"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆重量" prop="vehicleWeight">
        <el-input
          v-model="queryParams.vehicleWeight"
          placeholder="请输入车辆重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆颜色" prop="vehicleColor">
        <el-input
          v-model="queryParams.vehicleColor"
          placeholder="请输入车辆颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变速箱" prop="transmissionModel">
        <el-input
          v-model="queryParams.transmissionModel"
          placeholder="请输入变速箱型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂年份" prop="manufactureYear">
        <el-date-picker clearable
          v-model="queryParams.manufactureYear"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出厂年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="装载系统" prop="loadingSystem">
        <el-input
          v-model="queryParams.loadingSystem"
          placeholder="请输入装载系统"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保修时长" prop="warrantyTime">
        <el-date-picker clearable
          v-model="queryParams.warrantyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择保修时长">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="消耗能源" prop="consumptionEnergy">
        <el-input
          v-model="queryParams.consumptionEnergy"
          placeholder="请输入消耗能源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用/km" prop="costPerKilometer">
        <el-input
          v-model="queryParams.costPerKilometer"
          placeholder="请输入每公里费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人id" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人名" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['whole:inventory:add']"
          v-if="this.$store.state.user.name.charAt(0)=='C'"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['whole:inventory:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['whole:inventory:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whole:inventory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="车架号" align="center" prop="vin" />
      <el-table-column label="车的名称" align="center" prop="name" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="店端/厂端的库存" align="center" prop="inventoryType" />
      <el-table-column label="车型" align="center" prop="vehicleTypeName" />
      <el-table-column label="车辆尺寸" align="center" prop="vehicleSize" />
      <el-table-column label="车辆状态" align="center" prop="vehicleStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_dealer_vehicle_info" :value="scope.row.vehicleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆重量" align="center" prop="vehicleWeight" />
      <el-table-column label="车辆颜色" align="center" prop="vehicleColor" />
<!--      <el-table-column label="发动机型号" align="center" prop="engineType" />-->
      <el-table-column label="变速箱型号" align="center" prop="transmissionModel" />
      <el-table-column label="出厂年份" align="center" prop="manufactureYear" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufactureYear, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="装载系统" align="center" prop="loadingSystem" />
<!--      <el-table-column label="芯片类型" align="center" prop="chipType" />-->
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="保修时长" align="center" prop="warrantyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warrantyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消耗能源" align="center" prop="consumptionEnergy" />
      <el-table-column label="每公里费用" align="center" prop="costPerKilometer" />
      <el-table-column label="审核人id" align="center" prop="auditorId" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['whole:inventory:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['whole:inventory:remove']"
            v-show="scope.row.vehicleStatus==0"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆库存信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车架号" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入车架号(不输入，后台自动生成)" />
        </el-form-item>
        <el-form-item label="车的名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入车的名称" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="车型" prop="vehicleTypeName">
          <el-input v-model="form.vehicleTypeName" placeholder="请输入车型(SUV、小轿车)" />
        </el-form-item>
        <el-form-item label="车辆尺寸" prop="vehicleSize">
          <el-input v-model="form.vehicleSize" placeholder="请输入车辆尺寸，单位M" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="vehicleStatus">
          <el-radio-group v-model="form.vehicleStatus">
            <el-radio
              v-for="dict in dict.type.whole_dealer_vehicle_info"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆重量" prop="vehicleWeight">
          <el-input v-model="form.vehicleWeight" placeholder="请输入车辆重量，单位KG" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="vehicleColor">
          <el-input v-model="form.vehicleColor" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="变速箱" prop="transmissionModel">
          <el-input v-model="form.transmissionModel" placeholder="请输入变速箱型号(若不填写，后台自动生成)" />
        </el-form-item>
        <el-form-item label="出厂年份" prop="manufactureYear">
          <el-date-picker clearable
            v-model="form.manufactureYear"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出厂年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="装载系统" prop="loadingSystem">
          <el-input v-model="form.loadingSystem" placeholder="请输入装载系统" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格(单位W)" />
        </el-form-item>
        <el-form-item label="保修时长" prop="warrantyTime">
          <el-date-picker clearable
            v-model="form.warrantyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择保修时长">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消耗能源" prop="consumptionEnergy">
          <el-input v-model="form.consumptionEnergy" placeholder="请输入消耗能源(电、油)" />
        </el-form-item>
        <el-form-item label="费用/公里" prop="costPerKilometer">
          <el-input v-model="form.costPerKilometer" placeholder="请输入每公里费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory } from "@/api/whole/inventory";

export default {
  name: "Inventory",
  dicts: ['whole_dealer_vehicle_info'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆库存信息表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        vin: null,
        name: null,
        brand: null,
        inventoryType: null,
        vehicleTypeName: null,
        vehicleSize: null,
        vehicleStatus: null,
        vehicleWeight: null,
        vehicleColor: null,
        engineType: null,
        transmissionModel: null,
        manufactureYear: null,
        loadingSystem: null,
        chipType: null,
        price: null,
        warrantyTime: null,
        consumptionEnergy: null,
        costPerKilometer: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // vin: [
        //   { required: true, message: "车架号不能为空", trigger: "blur" }
        // ],
        name: [
          { required: true, message: "车的名称不能为空", trigger: "blur" }
        ],
        brand: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        inventoryType: [
          { required: true, message: "店端/厂端的库存不能为空", trigger: "change" }
        ],
        vehicleTypeName: [
          { required: true, message: "车型不能为空", trigger: "blur" }
        ],
        vehicleStatus: [
          { required: true, message: "车辆状态不能为空", trigger: "change" }
        ],
        manufactureYear: [
          { required: true, message: "出厂年份不能为空", trigger: "blur" }
        ],
        loadingSystem: [
          { required: true, message: "装载系统不能为空", trigger: "blur" }
        ],
        chipType: [
          { required: true, message: "芯片类型不能为空", trigger: "change" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆库存信息列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vin: null,
        name: null,
        brand: null,
        inventoryType: null,
        vehicleTypeName: null,
        vehicleSize: null,
        vehicleStatus: null,
        vehicleWeight: null,
        vehicleColor: null,
        engineType: null,
        transmissionModel: null,
        manufactureYear: null,
        loadingSystem: null,
        chipType: null,
        price: null,
        warrantyTime: null,
        consumptionEnergy: null,
        costPerKilometer: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆库存信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (this.$store.state.user.name.charAt(0)!='C'){
        this.$message.error('只有厂端才有权限删除！');
        return;
      }
      this.$modal.confirm('是否确认删除车辆库存信息编号为"' + ids + '"的数据项？').then(function() {
        return delInventory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('whole/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
